import com.dao.GradeDao;
import com.dao.ResultDao;
import com.pojo.Grade;
import com.pojo.QueryVo;
import com.pojo.Result;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wwj
 * @date 2021/1/11 0011 13:49
 */
public class mybatistest {
    public static void main(String[] args) throws IOException {
//        配置文件路径
        String resource = "mybatis-config.xml";
//        读取配置文件
        InputStream inputStream = Resources.getResourceAsStream(resource);
//        创建sqlsessionfaction工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//        创建SqlSession对象 参数true代表自动提交就省略掉了后面的sqlsesson.commit();实际开发中不常用，会直接使用spring的事务
        SqlSession sqlsesson = sqlSessionFactory.openSession(true);
//        创建接口的代理对象
        GradeDao gradeDao = sqlsesson.getMapper(GradeDao.class);
  //      ResultDao resultDao = sqlsesson.getMapper(ResultDao.class);


      //  查询
//        List<Grade> gradeList = gradeDao.findAll();
//        for(Grade grade : gradeList){
//            System.out.println(grade);
//        }


         //  根据id查询
//        Grade grade= gradeDao.queryByid(5);

        //查询条数
//        gradeDao.querycount();


         //根据名字模糊查询
//     List<Grade>  grades=gradeDao.queryByname("预");
//      for (Grade grade : grades){
//          System.out.println(grade);
//      }
    //根据名字和id查询
//        Grade grade = new Grade();
//        grade.setGradename("大一");
//        grade.setGradeid(1);
//        List<Grade> gradesList = gradeDao.queryByidAndName(grade);
//      for (Grade gradea : gradesList){
//          System.out.println(gradea);
//      }

//        //采用Vo查询
//        QueryVo vo = new QueryVo();
//        Grade grade = new Grade();
//        grade.setGradename("大");
//        vo.setGrade(grade);
//        List<Grade> gr = gradeDao.findGradeByVo(vo);
//        for (Grade grade1 : gr){
//            System.out.println(grade1);
//        }
//        //新增
//        Grade grade = new Grade();
//        grade.setGradeid(7);
//        grade.setGradename("王五");
//        gradeDao.saveGrade(grade);

        //修改
//        Grade grade = new Grade();
//        grade.setGradeid(7);
//        grade.setGradename("张三");
//        gradeDao.updateGrade(grade);



//        //批量删除
//        QueryVo vo = new QueryVo();
//        ArrayList<Integer>listIds=new ArrayList<>();
//        listIds.add(6);
//        listIds.add(7);
//        vo.setIds(listIds);
//        gradeDao.deleteListGrade(vo);




        //删除
//        gradeDao.deleteGrade(8);
//        释放资源


        sqlsesson.close();
        inputStream.close();
    }
}
